package com.member.web.controller.shop;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.member.common.annotation.Log;
import com.member.common.core.controller.BaseController;
import com.member.common.core.domain.AjaxResult;
import com.member.common.core.page.TableDataInfo;
import com.member.common.enums.BusinessType;
import com.member.common.utils.poi.ExcelUtil;
import com.member.shop.domain.SysBizRecommendGoods;
import com.member.shop.domain.SysBizShopSpecs;
import com.member.shop.service.ISysBizRecommendGoodsService;
import com.member.shop.vo.GoodsBindVo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * 【推荐分类与商品管理】控制器
 *
 * @author lixinchen
 * @version 1.0
 * @date 2025-01-08
 **/
@RestController
@RequestMapping("/shop/recommendGoods")
public class SysBizRecommendGoodsController extends BaseController {
    @Resource
    private ISysBizRecommendGoodsService sysBizRecommendGoodsService;

    private void setTenantId(SysBizRecommendGoods sysBizRecommendGoods) {
        Long tenantId = getTenantId();
        if (tenantId != null && tenantId > 0) {
            sysBizRecommendGoods.setTenantId(tenantId);
        }
    }

    /**
     * 查询推荐分类与商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('shop:recommendGoods:list')")
    @GetMapping("/list")
    public TableDataInfo list(SysBizRecommendGoods sysBizRecommendGoods) {
        startPage();
        setTenantId(sysBizRecommendGoods);
        List<SysBizRecommendGoods> list = sysBizRecommendGoodsService.selectSysBizRecommendGoodsList(sysBizRecommendGoods);
        return getDataTable(list);
    }


    @GetMapping("/getGoodsIds/{id}")
    public TableDataInfo getGoodsIds(@PathVariable("id") Long id) {
        List<SysBizRecommendGoods> sysBizRecommendGoods = sysBizRecommendGoodsService.selectSysBizRecommendGoodsList(
                new LambdaQueryWrapper<SysBizRecommendGoods>()
                        .eq(SysBizRecommendGoods::getRecommendCategoryId, id)
                        .eq(SysBizRecommendGoods::getDeleted, 0)
        );
        if(CollectionUtils.isEmpty(sysBizRecommendGoods)){
            return getDataTable(Stream.of(new Long[]{}).collect(Collectors.toList()));
        }
        List<Long> list = sysBizRecommendGoods.stream().map(SysBizRecommendGoods::getGoodsId).collect(Collectors.toList());
        return getDataTable(list);
    }

    /**
     * 导出推荐分类与商品管理列表
     */
    @PreAuthorize("@ss.hasPermi('shop:recommendGoods:export')")
    @Log(title = "推荐分类与商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysBizRecommendGoods sysBizRecommendGoods) {
        setTenantId(sysBizRecommendGoods);
        List<SysBizRecommendGoods> list = sysBizRecommendGoodsService.selectSysBizRecommendGoodsList(sysBizRecommendGoods);
        ExcelUtil<SysBizRecommendGoods> util = new ExcelUtil<SysBizRecommendGoods>(SysBizRecommendGoods.class);
        util.exportExcel(response, list, "推荐分类与商品管理数据");
    }

    /**
     * 获取推荐分类与商品管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('shop:recommendGoods:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(sysBizRecommendGoodsService.selectSysBizRecommendGoodsById(id));
    }


    /**
     * 修改排序
     */
    @PostMapping("/updateSort")
    public AjaxResult updateSort(@RequestBody SysBizRecommendGoods sysBizRecommendGoods) {
        return toAjax(sysBizRecommendGoodsService.update(new LambdaUpdateWrapper<SysBizRecommendGoods>()
                .eq(SysBizRecommendGoods::getId, sysBizRecommendGoods.getId())
                .set(SysBizRecommendGoods::getOrderNum, sysBizRecommendGoods.getOrderNum())));

    }

    /**
     * 新增推荐分类与商品管理
     */
    @PreAuthorize("@ss.hasPermi('shop:recommendGoods:add')")
    @Log(title = "推荐分类与商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysBizRecommendGoods sysBizRecommendGoods) {
        List<SysBizRecommendGoods> sysBizRecommendGoods1 = sysBizRecommendGoodsService.selectSysBizRecommendGoodsList(new LambdaQueryWrapper<SysBizRecommendGoods>()
                .eq(SysBizRecommendGoods::getRecommendCategoryId, sysBizRecommendGoods.getRecommendCategoryId())
                .eq(SysBizRecommendGoods::getTenantId, sysBizRecommendGoods.getTenantId())
                .eq(SysBizRecommendGoods::getGoodsId, sysBizRecommendGoods.getGoodsId()));
        if (!CollectionUtils.isEmpty(sysBizRecommendGoods1)) {
            return error("商品已绑定");
        }

        return toAjax(sysBizRecommendGoodsService.insertSysBizRecommendGoods(sysBizRecommendGoods));
    }

    @PostMapping("/remove")
    public AjaxResult remove(@RequestBody SysBizRecommendGoods sysBizRecommendGoods) {
        boolean remove = sysBizRecommendGoodsService.remove(new LambdaQueryWrapper<SysBizRecommendGoods>()
                .eq(SysBizRecommendGoods::getRecommendCategoryId, sysBizRecommendGoods.getRecommendCategoryId())
                .eq(SysBizRecommendGoods::getTenantId, sysBizRecommendGoods.getTenantId())
                .eq(SysBizRecommendGoods::getGoodsId, sysBizRecommendGoods.getGoodsId()));
        return toAjax(remove);
    }



    /**
     * 新增推荐分类与商品管理
     */
    @PreAuthorize("@ss.hasPermi('shop:recommendGoods:add')")
    @Log(title = "推荐分类与商品管理", businessType = BusinessType.INSERT)
    @PostMapping("/bindGoods")
    public AjaxResult bindGoods(@RequestBody GoodsBindVo goodsBindVo) {
        return toAjax(sysBizRecommendGoodsService.bindGoods(goodsBindVo));
    }


    @PreAuthorize("@ss.hasPermi('shop:recommendGoods:query')")
    @GetMapping(value = "/getBindGoods/{id}")
    public AjaxResult getBindGoods(@PathVariable("id") Long id) {

        List<SysBizRecommendGoods> sysBizRecommendGoods = sysBizRecommendGoodsService.selectSysBizRecommendGoodsList(new LambdaQueryWrapper<SysBizRecommendGoods>()
                .eq(SysBizRecommendGoods::getRecommendCategoryId, id));
        List<Long> collect = sysBizRecommendGoods.stream().map(SysBizRecommendGoods::getGoodsId).collect(Collectors.toList());
        return success(collect);
    }

    /**
     * 修改推荐分类与商品管理
     */
    @PreAuthorize("@ss.hasPermi('shop:recommendGoods:edit')")
    @Log(title = "推荐分类与商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysBizRecommendGoods sysBizRecommendGoods) {
        return toAjax(sysBizRecommendGoodsService.updateSysBizRecommendGoods(sysBizRecommendGoods));
    }

    /**
     * 删除推荐分类与商品管理
     */
    @PreAuthorize("@ss.hasPermi('shop:recommendGoods:remove')")
    @Log(title = "推荐分类与商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(sysBizRecommendGoodsService.deleteSysBizRecommendGoodsByIds(ids));
    }
}
